package com.util;

import java.sql.*;

public class BaseJdbc {

   public static   Connection connection =null;
   public static   PreparedStatement preparedStatement =null;
    static {
        //加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //获取连接http://47.100.193.239/
            String url="jdbc:mysql://47.100.193.239:3306/smbms";
            String uname="smbms";
            String password="123456";
            connection = DriverManager.getConnection(url,uname,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //通用的查询
    public static ResultSet BaseSelect(String sql,Object[] objects) throws Exception {
        preparedStatement = connection.prepareStatement(sql);
        if(objects[0] !=null){
            for (int i=0;i<objects.length;i++){
                preparedStatement.setObject(i+1,objects[i]);
            }
        }
        ResultSet resultSet = preparedStatement.executeQuery();
        return resultSet;
    }

    //通用的增删改
    public static int BaseUpdate(String sql,Object[] objects) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);
         for (int i=0;i<objects.length;i++){
                preparedStatement.setObject(i+1,objects[i]);
          }
        int i = preparedStatement.executeUpdate();
         return i;
    }
}
